import com.alibaba.fastjson.JSON;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.Result;
import com.baidu.unbiz.fluentvalidator.jsr303.HibernateSupportedValidator;
import com.baidu.unbiz.fluentvalidator.registry.Registry;
import com.tc.entity.UserInfo;
import com.tc.validator.CmapCustomerMerchantBasicInfoVO;
import com.tc.validator.MerchantCreate;
import lombok.extern.java.Log;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.validation.Validator;
import javax.validation.groups.Default;
import java.util.List;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toSimple;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/mvc-servlet.xml" }, inheritLocations = true)
@ActiveProfiles(value = "test")
public class testValidator {

    @Autowired
    private Validator validator;

    @Autowired
    private Registry springApplicationContextRegistry;

    @Test
    public void demo1(){
        CmapCustomerMerchantBasicInfoVO vo=new CmapCustomerMerchantBasicInfoVO();
        final HibernateSupportedValidator hibernateSupportedValidator = new HibernateSupportedValidator().setHiberanteValidator(validator);
        vo.setId("12123");
        vo.setMerchantChnName("--");
        final Result validateResult = FluentValidator.checkAll(MerchantCreate.class).configure(springApplicationContextRegistry)
                .putAttribute2Context("sourceFrom", "abc").on(vo, hibernateSupportedValidator)
                .when(vo != null).on(vo).doValidate().result(toSimple());
        System.out.println("验证结果:{}+"+ validateResult.toString());
        if (!validateResult.isSuccess()) {
            System.out.println(validateResult.getErrors().toString());
        }
    }
}
